package com.wzx.data.controller;

import com.wzx.basics.baseVo.PageVo;
import com.wzx.basics.baseVo.Result;
import com.wzx.basics.utils.PageUtil;
import com.wzx.basics.utils.ResultUtil;
import com.wzx.data.utils.NullUtils;
import com.wzx.data.entity.Video;
import com.wzx.data.service.IVideoService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 视频 前端控制器
 * @author wzx
 * @since 2023/03/20
 */
@Slf4j
@RestController
@Api(tags = "视频管理接口")
@RequestMapping("/wzx/video")
@Transactional
public class VideoController {

    @Autowired
    private IVideoService iVideoService;

    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ApiOperation(value = "查询单条视频")
    public Result<Video> get(@RequestParam String id){
        return new ResultUtil<Video>().setData(iVideoService.getById(id));
    }

    @RequestMapping(value = "/count", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部视频个数")
    public Result<Long> getCount(){
        return new ResultUtil<Long>().setData(iVideoService.count());
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部视频")
    public Result<List<Video>> getAll(){
        return new ResultUtil<List<Video>>().setData(iVideoService.list());
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "查询视频")
    public Result<IPage<Video>> getByPage(@ModelAttribute Video video ,@ModelAttribute PageVo page){
        QueryWrapper<Video> qw = new QueryWrapper<>();
        if(!NullUtils.isNull(video.getCreateBy())) {
        }
        IPage<Video> data = iVideoService.page(PageUtil.initMpPage(page),qw);
        return new ResultUtil<IPage<Video>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "增改视频")
    public Result<Video> saveOrUpdate(Video video){
        if(iVideoService.saveOrUpdate(video)){
            return new ResultUtil<Video>().setData(video);
        }
        return ResultUtil.error();
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value = "新增视频")
    public Result<Video> insert(Video video){
        iVideoService.saveOrUpdate(video);
        return new ResultUtil<Video>().setData(video);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "编辑视频")
    public Result<Video> update(Video video){
        iVideoService.saveOrUpdate(video);
        return new ResultUtil<Video>().setData(video);
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "删除视频")
    public Result<Object> delByIds(@RequestParam String[] ids){
        for(String id : ids){
            iVideoService.removeById(id);
        }
        return ResultUtil.success();
    }
}
